package com.jadcon.bedbreakfast.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.bedbreakfast.domain.Customer;
import com.jadcon.bedbreakfast.domain.CustomerService;
import com.jadcon.bedbreakfast.domain.Reservation;
import com.jadcon.bedbreakfast.domain.ReservationService;
import com.jadcon.bedbreakfast.domain.ReservationValidator;
import com.jadcon.bedbreakfast.domain.Suite;
import com.jadcon.bedbreakfast.domain.SuiteService;

@Controller
@SessionAttributes("newReservation")
public class AddReservationController {

	@Autowired
	private ReservationService reservationService;
	@Autowired
	private SuiteService suiteService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	Validator validator;
	
	public void setValidator(Validator validator) {
		this.validator = validator;
	}

	@RequestMapping("/addReservation.html")
	public ModelAndView addReservation(HttpSession session) {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (username.contentEquals("anonymousUser")){
			ModelAndView mav = new ModelAndView("redirect:bedbreakfast-webapp/spring_security_login");
			return mav;
		}
 
		Reservation newReservation = new Reservation();
		ModelAndView mav = new ModelAndView("addReservation");
		mav.addObject("newReservation", newReservation);
		session.setAttribute("newReservation", newReservation);
		List <Suite> suiteList = suiteService.getSuiteList();
		mav.addObject(suiteList);
		return mav;

	}
	
	@RequestMapping("/addReservationDateSpecific.html")
	public ModelAndView addReservation(@RequestParam String submit, HttpSession session) {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (username.contentEquals("anonymousUser")){
			ModelAndView mav = new ModelAndView("redirect:bedbreakfast-webapp/spring_security_login");
			return mav;
		}

		Reservation newReservation = new Reservation();
		ModelAndView mav = new ModelAndView("addReservationDateSpecific");
		mav.addObject("newReservation", newReservation);
		session.setAttribute("newReservation", newReservation);
		List <Suite> suiteList = suiteService.getSuiteList();
		mav.addObject(suiteList);
		return mav;

	}
	

	@RequestMapping("/addReservationConfirm.html")
	public ModelAndView confirmReservation(@RequestParam String submit,
			@ModelAttribute("newReservation") Reservation newReservation,
			BindingResult result, HttpSession session) {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Customer customer = customerService.findByUsername(username);
		
		validator.validate(newReservation, result);
		ReservationValidator reservationValidator = new ReservationValidator();
		reservationValidator.validate(newReservation, result);
		
		if (result.hasErrors()) {
			List <Suite> suiteList = suiteService.getSuiteList();
			ModelAndView mav = new ModelAndView("addReservation");
			mav.addObject(suiteList);
			return mav;
		}

		if (reservationService.SuiteAvailableForReservation(newReservation) == false) {
			return new ModelAndView("reservationSuiteNotAvailable");
		}

		ModelAndView mav = new ModelAndView("addReservationConfirm");
		mav.addObject("newReservation", newReservation);
		mav.addObject("customer", customer);
		return mav;
	}

	@RequestMapping("/saveReservation.html")
	public ModelAndView saveNewReservation(
			@ModelAttribute("newReservation") Reservation newReservation) {

		reservationService.save(newReservation);
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Customer customer = customerService.findByUsername(username);
		

		Reservation reservationWithNumber = new Reservation();
		reservationWithNumber = reservationService
				.getReservationNumberForNewlyCreatedReservation(newReservation);

		ModelAndView mav = new ModelAndView(
				"redirect:saveReservationConfirmation.html");
		mav.addObject("newReservation", reservationWithNumber);
		mav.addObject("customer", customer);
		return mav;
	}

	@RequestMapping("/saveReservationConfirmation.html")
	public ModelAndView saveNewReservationConfirmation(
			@ModelAttribute("newReservation") Reservation newReservation) {

		ModelAndView mav = new ModelAndView("saveReservationConfirmation");
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		Customer customer = customerService.findByUsername(username);
		
		mav.addObject("newReservation", newReservation);
		mav.addObject("customer", customer);
		return mav;
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}

}
